﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ERP_RORZE_TOOL.Common;
using ERP_RORZE_SYSTEM;

namespace ERP_RORZE_TOOL.Tool.BCThoigianlamviecthang
{
	public class BaseDAO
	{

		public static SqlConnection Connect()
		{
			SqlConnection connection = null;

			INISettings iniSettings = new INISettings();
			string connectString = iniSettings.SQLConnectionString;

			//string connectString = "server=.;uid=sa; pwd=123456; database=rrc_database;";	

			connection = new SqlConnection(connectString);
			try
			{
				connection.Open();
					return connection;
			}
			catch(Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return null;

			}

		}

		public static DataTable ExcuteProcedure(string storeProcedure)
		{
			SqlCommand command = new SqlCommand();
			SqlDataAdapter adapter = new SqlDataAdapter();
			DataTable dataTable = new DataTable();
			command.CommandType = CommandType.StoredProcedure;
			try
			{
				command.CommandText = storeProcedure;
				command.Parameters.Clear();
				command.Connection = Connect();
				adapter.SelectCommand = command;
				adapter.Fill(dataTable);
				Connect().Close();

				return dataTable;
			}
			catch (Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return null;
			}

			
		}

        public static DataTable ExcuteProcedureWithTempTable(string storeProcedure, string tempTableName)
		{
			SqlCommand command = new SqlCommand();
			SqlDataAdapter adapter = new SqlDataAdapter();
			DataTable dataTable = new DataTable();
			command.CommandType = CommandType.StoredProcedure;
			try
			{
				command.CommandText = ToolConstants.StoreProcedure.GetAllWithTempTable;
                command.Parameters.Add("@ProcName", SqlDbType.NVarChar).Value = storeProcedure;
                command.Parameters.Add("@TempTable", SqlDbType.NVarChar).Value = tempTableName;
				command.Connection = Connect();
				adapter.SelectCommand = command;
				adapter.Fill(dataTable);
				Connect().Close();

				return dataTable;
			}
			catch (Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return null;
			}

			
		}

		public static DataSet ExcuteProcedureDS(string storeProcedure)
		{
			SqlCommand command = new SqlCommand();
			SqlDataAdapter adapter = new SqlDataAdapter();
			DataSet ds = new DataSet();
			command.CommandType = CommandType.StoredProcedure;
			try
			{
				command.CommandText = storeProcedure;
				command.Parameters.Clear();
				command.Connection = Connect();
				
				adapter.SelectCommand = command;
				adapter.Fill(ds);
				Connect().Close();

				return ds;
			}
			catch (Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return null;
			}

			
		}

		public static DataTable ExcuteProcedure(string storeProcedure, SqlParameter[] parameters)
		{
			SqlCommand command = new SqlCommand();
			SqlDataAdapter adapter = new SqlDataAdapter();
			DataTable dataTable = new DataTable();

			command.CommandType = CommandType.StoredProcedure;
			command.CommandText = storeProcedure;
			try
			{
				foreach(SqlParameter sqlParameter in parameters)
				{
					command.Parameters.Add(sqlParameter);
				}

				command.Connection = Connect();
				adapter.SelectCommand = command;
				adapter.Fill(dataTable);
				Connect().Close();

				return dataTable;

			}
			catch(Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return null;

			}

		}

		//public static object ExcuteProcedureLoadOne(string storeProcedure, SqlParameter[] parameters)
		//{
		//	SqlCommand command = new SqlCommand();
		//	SqlDataAdapter adapter = new SqlDataAdapter();
		//	DataTable dataTable = new DataTable();

		//	command.CommandType = CommandType.StoredProcedure;
		//	command.CommandText = storeProcedure;
		//	try
		//	{
		//		foreach(SqlParameter sqlParameter in parameters)
		//		{
		//			command.Parameters.Add(sqlParameter);
		//		}

		//		command.Connection = Connect();
		//		adapter.SelectCommand = command;
		//		adapter.Fill(dataTable);
		//		Connect().Close();

		//		return dataTable;

		//	}
		//	catch(Exception e)
		//	{
		//		MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
		//		return null;

		//	}

		//}

		public static int ExecuteNonQueryProcedure(string storeProcedure, SqlParameter[] parameters)
		{
			SqlCommand command = new SqlCommand();
			command.CommandType = CommandType.StoredProcedure;
			command.CommandText = storeProcedure;
			int result = 0;
			try
			{
				foreach(SqlParameter sqlParameter in parameters)
				{
					command.Parameters.Add(sqlParameter);
				}

				command.Connection = Connect();
				result = command.ExecuteNonQuery();
				Connect().Close();

				return result;

			}
			catch(Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return result;
			}
		}

		public static int ExecuteNonQuery(string inputQuery)
		{
			SqlCommand command = new SqlCommand();
			command.CommandType = CommandType.Text;
            command.CommandText = inputQuery;
			int result = 0;
			try
			{
				command.Connection = Connect();
				result = command.ExecuteNonQuery();
				Connect().Close();

				return result;

			}
			catch(Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return result;
			}
		}

		public static DataTable ExecuteQuery(string inputQuery)
		{
			SqlCommand command = new SqlCommand();
		    SqlDataAdapter adapter;
			command.CommandType = CommandType.Text;
            command.CommandText = inputQuery;
			DataTable result = new DataTable();
			try
			{
				command.Connection = Connect();
                adapter = new SqlDataAdapter(command);
                adapter.Fill(result);
				Connect().Close();

				return result;

			}
			catch(Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return result;
			}
		}

		public static SqlParameter CreateParameter(string parameterName, SqlDbType type, object value)
		{
			SqlParameter parameter = new SqlParameter();

			try
			{
				parameter.ParameterName = parameterName;
				parameter.SqlDbType = type;
				parameter.Value = value;

				return parameter;
			}
			catch(Exception e)
			{
				MessageBox.Show("Error:"+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return null;
			}

		}

		public class MyException : Exception
		{
			public MyException(Exception e)
			{
				FileStream fs;
				StreamWriter sw;
				string date = DateTime.Now.ToString();
				int lineNum = Convert.ToInt32(e.StackTrace.Substring(e.StackTrace.LastIndexOf(":line") + 5));

				if(File.Exists("err.log"))
				{
					using(fs = new FileStream("err.log", FileMode.Append))
					{
						sw = new StreamWriter(fs);
						sw.WriteLine(string.Format("{0}: {1} :: {2}", date, e.Message, lineNum));
						sw.Close();
					}
				}
				else
				{
					using(fs = new FileStream("err.log", FileMode.OpenOrCreate))
					{
						sw = new StreamWriter(fs);
						sw.WriteLine(string.Format("{0}: {1} :: {2}", date, e.Message, lineNum));
						sw.Close();
					}
				}
			}
		}


	}

}
